﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace CodeNotes.DAL
{
    /// <summary>
    /// 数据访问类:Wx_QrcodeSite
    /// </summary>
    public partial class Wx_QrcodeSite
    {
        public Wx_QrcodeSite()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Wx_QrcodeSite");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CodeNotes.Model.Wx_QrcodeSite model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Wx_QrcodeSite(");
            strSql.Append("Openid,NickName,AvatarUrl,Company,Address,OpenidHours,Telephone,Introduce,CompanyImg1,CompanyImg2,CompanyImg3,Latitude,Longitude,QrcodeImg,CreateDate,UpdateDate,IsLock,TodayPageView,TotalPageView,LastPageViewTime)");
            strSql.Append(" values (");
            strSql.Append("@Openid,@NickName,@AvatarUrl,@Company,@Address,@OpenidHours,@Telephone,@Introduce,@CompanyImg1,@CompanyImg2,@CompanyImg3,@Latitude,@Longitude,@QrcodeImg,@CreateDate,@UpdateDate,@IsLock,@TodayPageView,@TotalPageView,@LastPageViewTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Openid", SqlDbType.VarChar,50),
					new SqlParameter("@NickName", SqlDbType.NVarChar,50),
					new SqlParameter("@AvatarUrl", SqlDbType.VarChar,200),
					new SqlParameter("@Company", SqlDbType.NVarChar,100),
					new SqlParameter("@Address", SqlDbType.NVarChar,100),
					new SqlParameter("@OpenidHours", SqlDbType.NVarChar,50),
					new SqlParameter("@Telephone", SqlDbType.NVarChar,50),
					new SqlParameter("@Introduce", SqlDbType.NVarChar,300),
					new SqlParameter("@CompanyImg1", SqlDbType.VarChar,200),
					new SqlParameter("@CompanyImg2", SqlDbType.VarChar,200),
					new SqlParameter("@CompanyImg3", SqlDbType.VarChar,200),
					new SqlParameter("@Latitude", SqlDbType.VarChar,50),
					new SqlParameter("@Longitude", SqlDbType.VarChar,50),
					new SqlParameter("@QrcodeImg", SqlDbType.VarChar,200),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@UpdateDate", SqlDbType.DateTime),
					new SqlParameter("@IsLock", SqlDbType.Bit,1),
					new SqlParameter("@TodayPageView", SqlDbType.Int,4),
					new SqlParameter("@TotalPageView", SqlDbType.Int,4),
					new SqlParameter("@LastPageViewTime", SqlDbType.DateTime)};
            parameters[0].Value = model.Openid;
            parameters[1].Value = model.NickName;
            parameters[2].Value = model.AvatarUrl;
            parameters[3].Value = model.Company;
            parameters[4].Value = model.Address;
            parameters[5].Value = model.OpenidHours;
            parameters[6].Value = model.Telephone;
            parameters[7].Value = model.Introduce;
            parameters[8].Value = model.CompanyImg1;
            parameters[9].Value = model.CompanyImg2;
            parameters[10].Value = model.CompanyImg3;
            parameters[11].Value = model.Latitude;
            parameters[12].Value = model.Longitude;
            parameters[13].Value = model.QrcodeImg;
            parameters[14].Value = model.CreateDate;
            parameters[15].Value = model.UpdateDate;
            parameters[16].Value = model.IsLock;
            parameters[17].Value = model.TodayPageView;
            parameters[18].Value = model.TotalPageView;
            parameters[19].Value = model.LastPageViewTime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CodeNotes.Model.Wx_QrcodeSite model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Wx_QrcodeSite set ");
            strSql.Append("Openid=@Openid,");
            strSql.Append("NickName=@NickName,");
            strSql.Append("AvatarUrl=@AvatarUrl,");
            strSql.Append("Company=@Company,");
            strSql.Append("Address=@Address,");
            strSql.Append("OpenidHours=@OpenidHours,");
            strSql.Append("Telephone=@Telephone,");
            strSql.Append("Introduce=@Introduce,");
            strSql.Append("CompanyImg1=@CompanyImg1,");
            strSql.Append("CompanyImg2=@CompanyImg2,");
            strSql.Append("CompanyImg3=@CompanyImg3,");
            strSql.Append("Latitude=@Latitude,");
            strSql.Append("Longitude=@Longitude,");
            strSql.Append("QrcodeImg=@QrcodeImg,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("UpdateDate=@UpdateDate,");
            strSql.Append("IsLock=@IsLock,");
            strSql.Append("TodayPageView=@TodayPageView,");
            strSql.Append("TotalPageView=@TotalPageView,");
            strSql.Append("LastPageViewTime=@LastPageViewTime");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Openid", SqlDbType.VarChar,50),
					new SqlParameter("@NickName", SqlDbType.NVarChar,50),
					new SqlParameter("@AvatarUrl", SqlDbType.VarChar,200),
					new SqlParameter("@Company", SqlDbType.NVarChar,100),
					new SqlParameter("@Address", SqlDbType.NVarChar,100),
					new SqlParameter("@OpenidHours", SqlDbType.NVarChar,50),
					new SqlParameter("@Telephone", SqlDbType.NVarChar,50),
					new SqlParameter("@Introduce", SqlDbType.NVarChar,300),
					new SqlParameter("@CompanyImg1", SqlDbType.VarChar,200),
					new SqlParameter("@CompanyImg2", SqlDbType.VarChar,200),
					new SqlParameter("@CompanyImg3", SqlDbType.VarChar,200),
					new SqlParameter("@Latitude", SqlDbType.VarChar,50),
					new SqlParameter("@Longitude", SqlDbType.VarChar,50),
					new SqlParameter("@QrcodeImg", SqlDbType.VarChar,200),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@UpdateDate", SqlDbType.DateTime),
					new SqlParameter("@IsLock", SqlDbType.Bit,1),
					new SqlParameter("@TodayPageView", SqlDbType.Int,4),
					new SqlParameter("@TotalPageView", SqlDbType.Int,4),
					new SqlParameter("@LastPageViewTime", SqlDbType.DateTime),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Openid;
            parameters[1].Value = model.NickName;
            parameters[2].Value = model.AvatarUrl;
            parameters[3].Value = model.Company;
            parameters[4].Value = model.Address;
            parameters[5].Value = model.OpenidHours;
            parameters[6].Value = model.Telephone;
            parameters[7].Value = model.Introduce;
            parameters[8].Value = model.CompanyImg1;
            parameters[9].Value = model.CompanyImg2;
            parameters[10].Value = model.CompanyImg3;
            parameters[11].Value = model.Latitude;
            parameters[12].Value = model.Longitude;
            parameters[13].Value = model.QrcodeImg;
            parameters[14].Value = model.CreateDate;
            parameters[15].Value = model.UpdateDate;
            parameters[16].Value = model.IsLock;
            parameters[17].Value = model.TodayPageView;
            parameters[18].Value = model.TotalPageView;
            parameters[19].Value = model.LastPageViewTime;
            parameters[20].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Wx_QrcodeSite ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Wx_QrcodeSite ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CodeNotes.Model.Wx_QrcodeSite GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,Openid,NickName,AvatarUrl,Company,Address,OpenidHours,Telephone,Introduce,CompanyImg1,CompanyImg2,CompanyImg3,Latitude,Longitude,QrcodeImg,CreateDate,UpdateDate,IsLock,TodayPageView,TotalPageView,LastPageViewTime from Wx_QrcodeSite ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            CodeNotes.Model.Wx_QrcodeSite model = new CodeNotes.Model.Wx_QrcodeSite();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CodeNotes.Model.Wx_QrcodeSite DataRowToModel(DataRow row)
        {
            CodeNotes.Model.Wx_QrcodeSite model = new CodeNotes.Model.Wx_QrcodeSite();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["Openid"] != null)
                {
                    model.Openid = row["Openid"].ToString();
                }
                if (row["NickName"] != null)
                {
                    model.NickName = row["NickName"].ToString();
                }
                if (row["AvatarUrl"] != null)
                {
                    model.AvatarUrl = row["AvatarUrl"].ToString();
                }
                if (row["Company"] != null)
                {
                    model.Company = row["Company"].ToString();
                }
                if (row["Address"] != null)
                {
                    model.Address = row["Address"].ToString();
                }
                if (row["OpenidHours"] != null)
                {
                    model.OpenidHours = row["OpenidHours"].ToString();
                }
                if (row["Telephone"] != null)
                {
                    model.Telephone = row["Telephone"].ToString();
                }
                if (row["Introduce"] != null)
                {
                    model.Introduce = row["Introduce"].ToString();
                }
                if (row["CompanyImg1"] != null)
                {
                    model.CompanyImg1 = row["CompanyImg1"].ToString();
                }
                if (row["CompanyImg2"] != null)
                {
                    model.CompanyImg2 = row["CompanyImg2"].ToString();
                }
                if (row["CompanyImg3"] != null)
                {
                    model.CompanyImg3 = row["CompanyImg3"].ToString();
                }
                if (row["Latitude"] != null)
                {
                    model.Latitude = row["Latitude"].ToString();
                }
                if (row["Longitude"] != null)
                {
                    model.Longitude = row["Longitude"].ToString();
                }
                if (row["QrcodeImg"] != null)
                {
                    model.QrcodeImg = row["QrcodeImg"].ToString();
                }
                if (row["CreateDate"] != null && row["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                }
                if (row["UpdateDate"] != null && row["UpdateDate"].ToString() != "")
                {
                    model.UpdateDate = DateTime.Parse(row["UpdateDate"].ToString());
                }
                if (row["IsLock"] != null && row["IsLock"].ToString() != "")
                {
                    if ((row["IsLock"].ToString() == "1") || (row["IsLock"].ToString().ToLower() == "true"))
                    {
                        model.IsLock = true;
                    }
                    else
                    {
                        model.IsLock = false;
                    }
                }
                if (row["TodayPageView"] != null && row["TodayPageView"].ToString() != "")
                {
                    model.TodayPageView = int.Parse(row["TodayPageView"].ToString());
                }
                if (row["TotalPageView"] != null && row["TotalPageView"].ToString() != "")
                {
                    model.TotalPageView = int.Parse(row["TotalPageView"].ToString());
                }
                if (row["LastPageViewTime"] != null && row["LastPageViewTime"].ToString() != "")
                {
                    model.LastPageViewTime = DateTime.Parse(row["LastPageViewTime"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Openid,NickName,AvatarUrl,Company,Address,OpenidHours,Telephone,Introduce,CompanyImg1,CompanyImg2,CompanyImg3,Latitude,Longitude,QrcodeImg,CreateDate,UpdateDate,IsLock,TodayPageView,TotalPageView,LastPageViewTime ");
            strSql.Append(" FROM Wx_QrcodeSite ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,Openid,NickName,AvatarUrl,Company,Address,OpenidHours,Telephone,Introduce,CompanyImg1,CompanyImg2,CompanyImg3,Latitude,Longitude,QrcodeImg,CreateDate,UpdateDate,IsLock,TodayPageView,TotalPageView,LastPageViewTime ");
            strSql.Append(" FROM Wx_QrcodeSite ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Wx_QrcodeSite ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from Wx_QrcodeSite T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Wx_QrcodeSite";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

